package com.xxwu.springlibrary.config;

import com.xxwu.springlibrary.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
//将返回值按照数据类型进行处理，若不加则默认是视图，返回数据类型时无法处理
@ResponseBody
public class ErrorHandler {

    @ExceptionHandler
    public Result handler(Exception e){
        log.error(e.getMessage());
        return Result.fail(e.getMessage(), "内部发生错误");
    }
}
